package com.lwbpro.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwbpro.common.exception.ServiceException;
import com.lwbpro.common.utils.DateUtils;
import com.lwbpro.system.domain.vo.ExamManageReqVO;
import com.lwbpro.system.domain.vo.LineRespVO;
import com.lwbpro.system.domain.vo.LineVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lwbpro.system.mapper.AchievementManageMapper;
import com.lwbpro.system.domain.AchievementManage;
import com.lwbpro.system.service.IAchievementManageService;

import javax.annotation.Resource;

/**
 * 学生考试成绩管理Service业务层处理
 *
 * @author achievement
 * @date 2024-09-02
 */
@Service
public class AchievementManageServiceImpl extends ServiceImpl<AchievementManageMapper, AchievementManage> implements IAchievementManageService {
    @Resource
    private AchievementManageMapper achievementManageMapper;

    /**
     * 查询学生考试成绩管理
     *
     * @param id 学生考试成绩管理主键
     * @return 学生考试成绩管理
     */
    @Override
    public AchievementManage selectAchievementManageById(Long id) {
        return achievementManageMapper.selectAchievementManageById(id);
    }

    /**
     * 查询学生考试成绩管理列表
     *
     * @param reqVO 学生考试成绩管理
     * @return 学生考试成绩管理
     */
    @Override
    public List<AchievementManage> selectAchievementManageList(ExamManageReqVO reqVO) {
        return achievementManageMapper.selectAchievementManageList(reqVO);
    }

    /**
     * 新增学生考试成绩管理
     *
     * @param achievementManage 学生考试成绩管理
     * @return 结果
     */
    @Override
    public int insertAchievementManage(AchievementManage achievementManage) {
        achievementManage.setCreateTime(DateUtils.getNowDate());
        return achievementManageMapper.insertAchievementManage(achievementManage);
    }

    /**
     * 修改学生考试成绩管理
     *
     * @param achievementManage 学生考试成绩管理
     * @return 结果
     */
    @Override
    public int updateAchievementManage(AchievementManage achievementManage) {
        achievementManage.setUpdateTime(DateUtils.getNowDate());
        return achievementManageMapper.updateAchievementManage(achievementManage);
    }

    /**
     * 批量删除学生考试成绩管理
     *
     * @param ids 需要删除的学生考试成绩管理主键
     * @return 结果
     */
    @Override
    public int deleteAchievementManageByIds(Long[] ids) {
        return achievementManageMapper.deleteAchievementManageByIds(ids);
    }

    /**
     * 删除学生考试成绩管理信息
     *
     * @param id 学生考试成绩管理主键
     * @return 结果
     */
    @Override
    public int deleteAchievementManageById(Long id) {
        return achievementManageMapper.deleteAchievementManageById(id);
    }

    @Override
    public List<BigDecimal> radar(Long id) {
        AchievementManage achievementManage = achievementManageMapper.selectAchievementManageById(id);
        if (achievementManage == null) {
            throw new ServiceException("考试成绩不存在");
        }
        List<BigDecimal> arrayList = new ArrayList<>();
        arrayList.add(achievementManage.getSubjectOne());
        arrayList.add(achievementManage.getSubjectTwo());
        arrayList.add(achievementManage.getSubjectThree());
        arrayList.add(achievementManage.getSubjectFour());
        arrayList.add(achievementManage.getSubjectFive());
        arrayList.add(achievementManage.getSubjectSix());
        arrayList.add(achievementManage.getSubjectSeven());
        return arrayList;
    }

    @Override
    public void updateExamManageByExamId(Long id, String name) {
        achievementManageMapper.updateExamManageByExamId(id, name);
    }

    @Override
    public void updateExamManageByStudentId(Long studentId, String studentName) {
        achievementManageMapper.updateExamManageByStudentId(studentId, studentName);
    }

    @Override
    public LineRespVO line(ExamManageReqVO reqVO) {
        List<LineVO> list = achievementManageMapper.line(reqVO);
        LineRespVO respVO = new LineRespVO();
        if (CollUtil.isNotEmpty(list)) {
            // 考试名称
            List<String> examNameList = new ArrayList<>();
            // 考试科目分数
            List<BigDecimal> subjectScoreList = new ArrayList<>();
            // 考试科目速度
            List<BigDecimal> subjectSpeedList = new ArrayList<>();
            list.forEach(item -> {
                examNameList.add(item.getExamName());
                subjectScoreList.add(item.getSubjectScore());
                subjectSpeedList.add(item.getSubjectSpeed());
            });

            respVO.setExamNameList(examNameList);
            respVO.setSubjectScoreList(subjectScoreList);
            respVO.setSubjectSpeedList(subjectSpeedList);
        }
        return respVO;
    }

    @Override
    public void deleteAchievementManageByStudentIds(Long[] studentIds) {
        achievementManageMapper.deleteAchievementManageByStudentIds(studentIds);
    }
}
